import 'package:enjoy_plus_ohos/utils/http.dart';
import 'package:enjoy_plus_ohos/utils/toast.dart';
import 'package:enjoy_plus_ohos/utils/token.dart';
import 'package:flutter/material.dart';
import 'package:enjoy_plus_ohos/pages/home/components/home_nav.dart';
import 'package:enjoy_plus_ohos/pages/home/components/home_list.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    // 获取公告列表数据
    getNotifyListData();
  }

  // 公告列表
  List notifyList = [];

  // 获取公告列表数据
  getNotifyListData() async {
    // 发送网络请求
    final res = await http.get('/announcement');
    // 成功轻提示
    ToastUtil.showInfo('数据加载成功');
    // 更新数据 + 视图
    setState(() {
      notifyList = res['data'];
    });
  }

  @override
  Widget build(BuildContext context) {
    // 定义背景色
    const Color bgColor = Color(0xFFE5E7F1); // 使用精确的背景色

    return Scaffold(
      backgroundColor: bgColor,
      appBar: AppBar(
        title: const Text('享+社区',
            style: TextStyle(
              color: Colors.black,
              fontWeight: FontWeight.bold,
              fontSize: 18,
            )),
        centerTitle: true,
        backgroundColor: bgColor,
        elevation: 0, // 确保没有阴影
      ),
      body: ListView(
        padding:
            const EdgeInsets.symmetric(horizontal: 15, vertical: 10), // 调整外边距
        children: [
          // 导航
          const HomeNav(),

          // 顶部间距
          const SizedBox(height: 15),

          // 公告图（带圆角）
          ClipRRect(
            borderRadius: BorderRadius.circular(10),
            child: Image.asset(
              'assets/images/banner@2x.jpg',
              fit: BoxFit.cover,
              width: double.infinity,
              height: 120, // 指定高度确保比例一致
            ),
          ),

          // 顶部间距
          const SizedBox(height: 15),

          // 公告列表
          HomeList(notifyList: notifyList),

          // 测试按钮
          FilledButton(
            onPressed: () async {
              final res = await http.get('/announcement');
              print(res);
            },
            child: const Text('测试 dio 网络请求'),
          ),
          FilledButton(
            onPressed: () async {
              try {
                final res = await http.get('/error');
                print(res);
              } catch (e) {
                print(e);
              }
            },
            child: const Text('测试 dio 网络请求-失败'),
          ),
          FilledButton(
            onPressed: () {
              ToastUtil.showInfo('轻提示-蓝色');
            },
            child: const Text('测试轻提示'),
          ),
          FilledButton(
            style: FilledButton.styleFrom(
              backgroundColor: Colors.green,
            ),
            onPressed: () {
              ToastUtil.showSuccess('轻提示-绿色');
            },
            child: const Text('测试轻提示-成功'),
          ),
          FilledButton(
            style: FilledButton.styleFrom(
              backgroundColor: Colors.red,
            ),
            onPressed: () {
              ToastUtil.showError('轻提示-红色');
            },
            child: const Text('测试轻提示-失败'),
          ),
          // 测试代码
          FilledButton(
            onPressed: () async {
              await TokenManager().saveToken('itheima_token');
              ToastUtil.showInfo('存储成功');
            },
            child: const Text('测试持久化-存token'),
          ),
          FilledButton(
            onPressed: () {
              final token = TokenManager().getToken();
              ToastUtil.showInfo(token ?? '');
            },
            child: const Text('测试持久化-取token'),
          ),
          FilledButton(
            onPressed: () async {
              await TokenManager().removeToken();
              ToastUtil.showInfo('删除成功');
            },
            child: const Text('测试持久化-删token'),
          ),
          FilledButton(
            onPressed: () async {
              Navigator.pushNamed(context, '/profile');
            },
            child: const Text('去个人信息页'),
          ),
          FilledButton(
            onPressed: () async {
              try {
                final res = await http.get('/userInfo');
                print(res);
              } catch (e) {
                print(e);
              }
            },
            child: const Text('测试需要带 token 的请求'),
          ),
        ],
      ),
    );
  }
}
